package cn.edu.zut.qixiang.service;

import cn.edu.zut.qixiang.dto.DistrictWeatherDTO;
import cn.edu.zut.qixiang.dto.MapWeatherDTO;
import cn.edu.zut.qixiang.mapper.WeatherMapMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86178
 */
@Service
public class WeatherMapServiceImpl implements WeatherMapService {

    @Autowired
    private WeatherMapMapper weatherMapMapper;

    @Override
    public Map<String, Object> getWeatherMapData(String date) {
        List<MapWeatherDTO> mapData = weatherMapMapper.selectWeatherMapData(date);

        // 转换数据格式为前端需要的结构
        List<Map<String, Object>> convertedData = convertToFrontendFormat(mapData);

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "成功");
        result.put("data", convertedData);
        return result;
    }

    @Override
    public Map<String, Object> getDistrictWeatherInfo(String district, String date) {
        DistrictWeatherDTO districtInfo = weatherMapMapper.selectDistrictWeatherInfo(district, date);

        // 转换单个区域的数据格式
        Map<String, Object> convertedData = convertDistrictToFrontendFormat(districtInfo);

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "成功");
        result.put("data", convertedData);
        return result;
    }

    /**
     * 将数据库DTO转换为前端需要的格式
     */
    private List<Map<String, Object>> convertToFrontendFormat(List<MapWeatherDTO> mapData) {
        List<Map<String, Object>> result = new ArrayList<>();

        for (MapWeatherDTO dto : mapData) {
            Map<String, Object> converted = new HashMap<>();

            // 设置基本信息
            converted.put("name", dto.getName());

            // 设置value数组 [经度, 纬度, 温度]
            Double[] coordinates = new Double[]{
                    dto.getLongitude(),
                    dto.getLatitude(),
                    dto.getAvgTemperature()
            };
            converted.put("value", coordinates);

            // 设置详细气象信息
            Map<String, Object> detail = new HashMap<>();
            detail.put("weather", dto.getWeather());
            detail.put("weatherImage", getWeatherImage(dto.getWeather()));
            detail.put("temperature", String.format("%.1f°C", dto.getAvgTemperature()));
            detail.put("sunshine", String.format("%.1f小时", dto.getSunshineDuration()));
            detail.put("airQuality", convertAirQuality(dto.getAirQuality()));
            detail.put("uvIntensity", dto.getUvIntensity());
            detail.put("humidity", dto.getHumidity() + "%");
            detail.put("feelsLike", String.format("%.1f°C", dto.getFeelsLike()));

            converted.put("detail", detail);

            result.add(converted);
        }

        return result;
    }

    /**
     * 转换单个区域的数据格式
     */
    private Map<String, Object> convertDistrictToFrontendFormat(DistrictWeatherDTO dto) {
        Map<String, Object> converted = new HashMap<>();

        if (dto == null) {
            return converted;
        }

        // 设置基本信息
        converted.put("name", dto.getName());

        // 设置value数组 [经度, 纬度, 温度]
        Double[] coordinates = new Double[]{
                dto.getLongitude(),
                dto.getLatitude(),
                dto.getAvgTemperature()
        };
        converted.put("value", coordinates);

        // 设置详细气象信息
        Map<String, Object> detail = new HashMap<>();
        detail.put("weather", dto.getWeather());
        detail.put("weatherImage", getWeatherImage(dto.getWeather()));
        detail.put("temperature", String.format("%.1f°C", dto.getAvgTemperature()));
        detail.put("sunshine", String.format("%.1f小时", dto.getSunshineDuration()));
        detail.put("airQuality", convertAirQuality(dto.getAirQuality()));
        detail.put("uvIntensity", dto.getUvIntensity());
        detail.put("humidity", dto.getHumidity() + "%");
        detail.put("feelsLike", String.format("%.1f°C", dto.getFeelsLike()));

        converted.put("detail", detail);

        return converted;
    }

    /**
     * 根据天气类型获取对应的图片路径
     */
    private String getWeatherImage(String weather) {
        if (weather == null) {
            return "images/cloudy.png";
        }

        Map<String, String> weatherImageMap = new HashMap<>();
        weatherImageMap.put("晴", "images/sunny.png");
        weatherImageMap.put("多云", "images/cloudy.png");
        weatherImageMap.put("阴", "images/overcast.png");
        weatherImageMap.put("小雨", "images/light_rain.png");
        weatherImageMap.put("中雨", "images/middle_rain.png");
        weatherImageMap.put("大雨", "images/heavy_rain.png");
        weatherImageMap.put("暴雨", "images/rainstorm.png");
        weatherImageMap.put("雾", "images/fog.png");
        weatherImageMap.put("霾", "images/haze.png");

        return weatherImageMap.getOrDefault(weather, "images/cloudy.png");
    }

    /**
     * 转换空气质量描述
     */
    private String convertAirQuality(String quality) {
        if (quality == null) {
            return "一般";
        }

        Map<String, String> qualityMap = new HashMap<>();
        qualityMap.put("优", "优");
        qualityMap.put("良", "良");
        qualityMap.put("中", "一般");
        qualityMap.put("差", "差");

        return qualityMap.getOrDefault(quality, "一般");
    }
}